<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/15
 * Time: 15:49
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class History extends Base
{
    /**
     * @return mixed|\think\response\Json
     * 首页展示
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $params = input('param.');
            $where = [];
            //用户身份
            if(isset($params['user_type']) && $params['user_type'] != ''){
                $where['user_type'] = $params['user_type'];
            }
            //房屋类型
            if(isset($params['h_type']) && $params['h_type'] != ''){
                $where['h_type'] = $params['h_type'];
            }
            //用户姓名
            if(isset($params['user_username']) && $params['user_username'] != ''){
                $where['user_username'] = ['like', '%' . $params['user_username'] . '%'];
            }
            //房源名称
            if(isset($params['h_title']) && $params['h_title'] != ''){
                $where['h_title'] = ['like', '%' . $params['h_title'] . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('history')
                ->join('up_u_user', 'user_id = his_user_id', 'left')
                ->join('up_house', 'h_id = his_h_id', 'left')
                ->join('up_area ua1', 'ua1.district_id = h_sheng', 'left')
                ->join('up_area ua2', 'ua2.district_id = h_shi', 'left')
                ->join('up_area ua3', 'ua3.district_id = h_qu', 'left')
                ->join('up_area ua4', 'ua4.district_id = h_street', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('history')
                ->join('up_u_user', 'user_id = his_user_id', 'left')
                ->join('up_house', 'h_id = his_h_id', 'left')
                ->join('up_area ua1', 'ua1.district_id = h_sheng', 'left')
                ->join('up_area ua2', 'ua2.district_id = h_shi', 'left')
                ->join('up_area ua3', 'ua3.district_id = h_qu', 'left')
                ->join('up_area ua4', 'ua4.district_id = h_street', 'left')
                ->field('user_id, user_username, user_image, user_type, his_date, h_img, h_title, 
                h_xiaoqu, ua1.district as province, ua2.district as city, ua3.district as district, 
                ua4.district as street, h_price, h_shis, h_tings, h_weis, h_mianji, h_type, his_id')
                ->where($where)
                ->page($Nowpage, $limits)
                ->order('his_date desc')
                ->select();
            foreach($lists as $key => &$v){
                //详细地址
                $v['address'] = $v['province'] . $v['city'] . $v['district'] . $v['street'];
                //户型
                $v['huxing'] = $v['h_shis'] . '室' . $v['h_tings'] . '厅' . $v['h_weis'] . '卫';
                //价格/租金
                if($v['h_type'] == 1){
                    $v['zujin'] = $v['h_price'];
                    $v['price'] = '';
                }else{
                    $v['zujin'] = '';
                    $v['price'] = $v['h_price'];
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * @return mixed
     * 足迹详情
     */
    public function historyDetail(){
        $his_id = input('id');
        $info = Db::name('history')
            ->join('up_u_user', 'user_id = his_user_id', 'left')
            ->join('up_house', 'h_id = his_h_id', 'left')
            ->join('up_area ua1', 'ua1.district_id = h_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = h_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = h_qu', 'left')
            ->join('up_area ua4', 'ua4.district_id = h_street', 'left')
            ->field('user_id, user_username, user_image, user_type, his_date, h_img, h_title, 
                h_xiaoqu, ua1.district as province, ua2.district as city, ua3.district as district, 
                ua4.district as street, h_price, h_shis, h_tings, h_weis, h_mianji, h_type, h_images')
            ->where('his_id', $his_id)
            ->find();
        //图片
        if(!empty($info['h_images'])){
            $info['h_images'] = explode(',', $info['h_images']);
        }
        //详细地址
        $info['address'] = $info['province'] . $info['city'] . $info['district'] . $info['street'];
        //户型
        $info['huxing'] = $info['h_shis'] . '室' . $info['h_tings'] . '厅' . $info['h_weis'] . '卫';
        $this->assign('info', $info);
        return $this->fetch('history/historydetail');
    }
}